import Mock from 'mockjs';
import qs from 'query-string';
import setupMock from '@/utils/setupMock';
const Random = Mock.Random;

const { list } = Mock.mock({
  'list|100': [
    {
      _id: /[0-9]{8}[-][0-9]{4}/,
      'uid|10': /[A-Z][a-z][-][0-9]/,
      'provider|1': ['local', 'github'],
      email: Random.email(),
      nickName: Mock.mock('@cname'),
      avatar: Random.image('200x100', '#FF6600'),
      "targetReplayId": "21312",
      "targetReplayContent": "嗯，嗯",
      "currentReplayContent": "好，不错",
      "commentTime": 1611745373,
      "auditTime": 1637558682,
      "auditStatus|1": ['1', '2', '3'],
      "articleId": "601134b4c4ae0128013d322d",
      "articleTitle": "测试评论文章"
    },
  ],
});



setupMock({
  setup: () => {
    Mock.mock(new RegExp('/api/comment'), 'get', (params) => {
      console.log(params)
      const pageDate = JSON.parse(params.body)
      const p = pageDate.page as number;
      const ps = pageDate.pageSize as number;

      return {
        code: 200,
        msg: '请求成功',
        data: {
          list: list.slice((p - 1) * ps, p * ps),
          total: list.length,
        }
      };
    });

    Mock.mock(new RegExp('/api/comment'), 'delete', (params) => {
      const _id = params.body
      const index = list.findIndex(item => item._id === _id);
      list.splice(index, 1)
      return {
        code: 200,
        msg: '删除成功',
        data: {}
      };
    });

    Mock.mock(new RegExp('/api/comment'), 'put', (params) => {
      console.log(params)
      const data = JSON.parse(params.body)
      const index = list.findIndex(item => item._id === data._id);
      if (list[index]) list[index].auditStatus = data.auditStatus
      return {
        code: 200,
        msg: '操作成功',
        data: {}
      };
    });

  },
});
